package com.ptj.service;


import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import com.ptj.dao.UserMapper;
import com.ptj.entity.User;

@Service
@Transactional
public class UserServiceImpl {
	@Resource
	private UserMapper ump;
	
	//登录验证
	public User userLogin(@Param("userphone")String userphone,@Param("password")String password) {
		return this.ump.userLogin(userphone, password);
	}

	//用户注册
	public void register(User user) {
		this.ump.userRegister(user);
	}
	
	//用户删除
	public int deleteUser(Integer id) {
		return this.ump.deteleUser(id);
	}
	
	//用户完善信息
	public void addUser(User user) {
		this.ump.addUser(user);
	}

	//根据手机号查用户
	public User selectUserByPhone(@Param("userphone")String userphone) {	
		return this.ump.selectUserByPhone(userphone);
		
	}
	
	//根据用户名查用户
	public User selectUserByName(String username) {
		return this.ump.selectUserByName(username);
	}
	/*
	 * 注册jsp传入数据验证
	 */
	public static Map<String,Object> getValidator(BindingResult re){
		//解决异常，返回null与错误信息
				Map<String,Object> map=null;
				//判断是否有错
				boolean flag = re.hasErrors();
				if(flag){
					map=new HashMap<String,Object>();
					for(FieldError error:re.getFieldErrors()){
						//获取错误属性
						String field = error.getField();
						//获取属性的错误信息
						String mess=error.getDefaultMessage();
						map.put(field, mess);
						}
					return map;
				}
				return null;
 
	}
	/*
	 * 用户信息修改
	 */
	public void updateUser(User user) {
		this.ump.updateUser(user);
	}
	/*
	 * 根据id查
	 */

	public User selectUserByid(Integer id) {
		  return this.ump.selectUserByid(id);
			}

}
